package listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import config.Config;
import config.SocketConfig;

import service.AnswerBroadSender;
import ui.QuestionNumberUI;
import util.Java_JDBC;
import util.WordReader;

/**
 * 实现接口ActionListener
 * 
 * @author wj
 * 
 */
public class QuestionNumberListener implements ActionListener {
	private JTextField singleStart;
	private JTextField singleEnd;
	private JTextField multipleStart;
	private JTextField multipleEnd;
	private JTextField digitalStart;
	private JTextField digitalEnd;

	private QuestionNumberUI questionUI;
	private JButton jb1, jb2,jb3;
	private JComboBox<Object> ca;

	public QuestionNumberListener(QuestionNumberUI questionUI) {
		this.questionUI = questionUI;
		this.ca = questionUI.ca;

		this.singleStart = questionUI.singleStart;
		this.singleEnd = questionUI.singleEnd;

		this.multipleStart = questionUI.multipleStart;
		this.multipleEnd = questionUI.multipleEnd;

		this.digitalStart = questionUI.digitalStart;
		this.digitalEnd = questionUI.digitalEnd;

		this.jb1 = questionUI.jb1;
		this.jb2 = questionUI.jb2;
		this.jb3 = questionUI.jb3;
	}

	/**
	 * 对输入的信息进行判断 输出结果
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jb1) {
			if (!singleStart.getText().equals("")) {
				Config.singleStart = Integer.parseInt(singleStart.getText());
			}
			if (!singleEnd.getText().equals("")) {
				Config.singleEnd = Integer.parseInt(singleEnd.getText());
			}
			if (!multipleStart.getText().equals("")) {
				Config.multipleStart = Integer
						.parseInt(multipleStart.getText());
			}
			if (!multipleEnd.getText().equals("")) {
				Config.multipleEnd = Integer.parseInt(multipleEnd.getText());
			}
			if (!digitalStart.getText().equals("")) {
				Config.digitalStart = Integer.parseInt(digitalStart.getText());
			}
			if (!digitalEnd.getText().equals("")) {
				Config.digitalEnd = Integer.parseInt(digitalEnd.getText());
			}
			if (Config.paperName == null || Config.paperName.equals(""))
				return;
			if (Config.SelectedList.contains(Config.paperName)) {
//				JFrame jf1 = new JFrame("试卷选择失败");
//				jf1.setSize(200, 150);
//				jf1.setLocationRelativeTo(null);
//				JLabel jl_user = new JLabel("对不起，此试卷已经完成解答");
//				jf1.add(jl_user);
//				jf1.setVisible(true);
				
				JOptionPane.showMessageDialog(questionUI, "对不起，此试卷已经完成解答");  
				return;
			}
			JOptionPane.showMessageDialog(questionUI, "开始作答试卷："+Config.paperName); 
			Config.SelectedList.add(Config.paperName);
			Config.isAnswer = true;
			String sendMsg = "startAnswer:" + Config.singleStart + ":"
					+ Config.singleEnd + ":" + Config.multipleStart + ":"
					+ Config.multipleEnd + ":" + Config.digitalStart + ":"
					+ Config.digitalEnd + ":" + Config.paperName;
			System.out.println("sendMsg: " + sendMsg);
			Thread thread = new Thread(new AnswerBroadSender(sendMsg,
					SocketConfig.answerBroadPort));
			thread.start();
			questionUI.dispose();
		} else if (e.getSource() == jb2) {
			questionUI.dispose();
			Config.paperName = "";
		}else if(e.getSource() == jb3) {
			try {
				String paperPath = new Java_JDBC()
						.getPaperPath(Config.paperName);
				String paperNames[] = Config.paperName.split("\\.");
				System.out.println(paperNames[0] + "   " + paperNames[1]);
				Thread thread = new Thread(new WordReader(paperPath,
						paperNames[1]));
				thread.start();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
}
